#pragma once

#include <stdarg.h>
#include <iostream>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define SIZE 1024

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen    1
#define Onefile   2
#define Classfile 3

#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        printMethod = Screen;
        path = "./log/";
    }
    void Enable(int method)
    {
        printMethod = method;
    }
    std::string levelToString(int level)
    {
        switch(level)
        {
            case Info : return "Info";
            case Debug : return "Debug";
            case Warning : return "Warning";
            case Error : return "Error";
            case Fatal : return "Fatal";
            default : return "None";
        }
    }
    void operator()(int level, char* format, ...)
    {
        char leftbuffer[SIZE];
        time_t t = time(nullptr);
        struct tm* ctime = localtime(&t);
        snprintf(leftbuffer, SIZE,
            "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), 
            ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        char rightbuffer[SIZE];
        va_list s;
        va_start(s, format);
        vsnprintf(rightbuffer, SIZE, format, s);
        va_end(s);
        
        char logtxt[2*SIZE];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
        //std::cout << logtxt << std::endl;
        PrintLog(level, logtxt);
    }   
    // void logmessage(int level, char* format, ...)
    // {
    //     char leftbuffer[SIZE];
    //     time_t t = time(nullptr);
    //     struct tm* ctime = localtime(&t);
    //     snprintf(leftbuffer, SIZE,
    //         "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), 
    //         ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
    //     char rightbuffer[SIZE];
    //     va_list s;
    //     va_start(s, format);
    //     vsnprintf(rightbuffer, SIZE, format, s);
    //     va_end(s);
        
    //     char logtxt[2*SIZE];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
    //     //std::cout << logtxt << std::endl;
    //     PrintLog(level, logtxt);
    // }   
    void PrintLog(int level, const std::string& logtxt)
    {
        switch(printMethod)
        {
            case Screen:
                std::cout << logtxt << std::endl;
                break;
            case Onefile:
                PrintOnefile(LogFile , logtxt);
                break;
            case Classfile:
                PrintClassfile(level, logtxt);
                break;
            default: break;
        }
    }
    void PrintOnefile(const std::string& filename, const std::string& logtxt)
    {
        std::string logname = path + filename;
        int fd = open(logname.c_str(), O_WRONLY|O_CREAT|O_APPEND, 0666);
        if(fd < 0)
        {
            return;
        }
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }
    void PrintClassfile(int level, const std::string& logtxt)
    {
        std::string filename = LogFile;
        filename += ".";
        filename += levelToString(level);
        PrintOnefile(filename, logtxt);
    }
    ~Log()
    {}
private:
    int printMethod;
    std::string path;
};


